import { loginAPI, getUserInfoAPI } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
// import { setTimeout } from 'core-js'
export default {
  state: {
    token: getToken(),
    userInfo: {}// 存储用户信息
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // 同步到持久化
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      // 删除本地存储
      removeToken()
    },
    setUserInfo(state, preload) {
      state.userInfo = preload
    }
  },
  actions: {
    async login(store, data) {
      // console.log(data)
      // setTimeout(() => {
      //   store.commit('setToken', '111111')// 提交Token
      // }, 500)
      // const token = await loginAPI(data)
      // store.commit('setToken', token)
      store.commit('setToken', await loginAPI(data))
    },
    async getUserInfo(store) {
      store.commit('setUserInfo', await getUserInfoAPI())
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('setUserInfo', {})
    }
  },
  namespaced: true,
  getters: {
  }
}
